import pika
import sys
#接收方是模糊查询

connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
#声明交换机
channel.exchange_declare(exchange='topic_logs', exchange_type='topic')
#创建一个具有专属标志的随机队列，一旦消费者连接关闭，队列就被删除
result = channel.queue_declare('', exclusive=True)
queue_name = result.method.queue

binding_keys = '#.cj.#'

for binding_key in binding_keys:
    channel.queue_bind(
        exchange='topic_logs', queue=queue_name, routing_key=binding_key)

print(' [*] Waiting for logs. To exit press CTRL+C')


def callback(ch, method, properties, body):
    print(" [x] %r:%r" % (method.routing_key, body))
    ch.basic_publish(exchange='topic_logs',routing_key=binding_key,properties=pika.BasicProperties(correlation_id = \
                                                         properties.correlation_id),body='6666666666666')


channel.basic_qos(prefetch_count=1)
channel.basic_consume(
    queue=queue_name, on_message_callback=callback, auto_ack=True)

channel.start_consuming()